package com.loyal.loyalerp.humenresource.contract.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.humenresource.contract.entity.Contract;
import com.loyal.loyalerp.humenresource.contract.entity.ContractPageInfo;
import com.loyal.loyalerp.humenresource.contract.entity.ReturnMsg;
import com.loyal.loyalerp.humenresource.contract.service.ContractService;

@Controller
@RequestMapping("contract")
public class ConstractController {
	@Autowired
	private ContractService service;

	// 进入到显示劳动合同的页面
	@RequestMapping("enterContract")
	public String enterContract() {
		return "HumenResource/contract/constract";
	}

	// 进入增加劳动合同的页面
	@RequestMapping("enterAddContract")
	public String enterAddContract() {
		return "HumenResource/contract/add";
	}

	// 进入编辑劳动合同的页面
	@RequestMapping("enterEdit")
	public String enterEditContract() {
		return "HumenResource/contract/editcontract";
	}

	// 进入搜索劳动合同的页面
	@RequestMapping("search")
	public String search() {
		return "HumenResource/contract/search";
	}

	// 获得每个用户所有劳动合同信息
	@RequestMapping("getAllContract")
	public @ResponseBody ContractPageInfo getAllContract(
			@RequestParam(name = "_search", required = false) String search,
			@RequestParam(name = "rows", required = false) String rows,
			@RequestParam(name = "page", required = false) String page,
			@RequestParam(name = "searchField", required = false) String searchField,
			@RequestParam(name = "searchString", required = false) String searchString) {
		if (search.equals("false")) {
			ContractPageInfo result = new ContractPageInfo();
			PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
			List<Contract> list = service.getAllContract();
			PageInfo<Contract> info = new PageInfo<Contract>(list);
			result.setPage(info.getPageNum() + "");
			result.setRows(list);
			result.setTotal(info.getPages() + "");
			return result;
		} else {
			if (searchField.equals("username")) {
				ContractPageInfo result = new ContractPageInfo();
				PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
				List<Contract> list = service.searchContractByName(searchString);
				PageInfo<Contract> info = new PageInfo<Contract>(list);
				result.setPage(info.getPageNum() + "");
				result.setRows(list);
				result.setTotal(info.getPages() + "");
				return result;
			} else {
				return null;
			}
		}
	}

	// 添加合同
	@RequestMapping("addContract")
	public @ResponseBody ReturnMsg addContract(Contract contract) {
		ReturnMsg msg = null;
		try {
			msg = service.addContract(contract);
		} catch (Exception e) {
			msg = new ReturnMsg();
			msg.setStatus(400);
			msg.setMessage("后台处理失败:" + e.getMessage());
		}
		return msg;
	}

	// 删除一个合同
	@RequestMapping("deleteContract")
	public @ResponseBody ReturnMsg deleteContract(String id,
			@RequestParam(name = "oper", required = false) String oper) {
		if (oper.equals("del")) {
			ReturnMsg msg = null;
			try {
				service.deleteContract(Integer.parseInt(id));
				msg = new ReturnMsg();
				msg.setStatus(200);
				msg.setMessage("删除成功");
			} catch (Exception e) {
				msg = new ReturnMsg();
				msg.setStatus(400);
				msg.setMessage("后台处理失败:" + e.getMessage());
			}
			return msg;
		} else {
			return null;
		}
	}
    //修改合同信息
	@RequestMapping("updateContract")
	public @ResponseBody ReturnMsg updateContract(Contract contract) {
		ReturnMsg msg = null;
		try {
			msg = service.updateContract(contract);
			
		}
		catch (Exception e) {
			msg = new ReturnMsg();
			msg.setStatus(400);
			System.out.println(e.getMessage());
			msg.setMessage("修改失败"+e.getMessage());
		}
		return msg;
	}
}
